#include "./project.h"
bool main_game(LCD_P lcd)
{
    printf("1-------------\n");
    // 创建游戏的结构体，初始化
    game_info_bash(lcd);
    printf("1-------------\n");
    show_white_screen(lcd);
    printf("1-------------\n");
    // 显示按钮功能界面
    Show_One_Thumbnail(lcd, "/ui/game.bmp", 1, 720, 0);
    printf("1-------------\n");
    // 圆心获取随机值
    get_rand(lcd);
    printf("1-------------\n");
    // 刷一个球出来
    show_one_ball(lcd);
    printf("1-------------\n");
    // 刷一个静态木板
    show_board_picter(lcd);
    printf("1-------------\n");
    // 分数
    line(lcd, "score:0", 330, 0);
    printf("1-------------\n");
    // 创建球的线程
    pthread_create(&ball_run, NULL, ball_running, (void *)lcd);
    printf("1-------------\n");
    // 动态木板的实现
    show_board(lcd);
    printf("1-------------\n");

    return true;
}
bool show_board(LCD_P lcd)
{
    struct input_event touch;
    int all_x = -1;
    int all_y = -1;
    int btn_x = -1;
    int btn_y = -1;
    while (1)
    {
        // 获取实时坐标并得到木板边界判断的值。
        if (read(lcd->fp_input, &touch, sizeof(touch)) == -1)
        {
            perror("read touch lcd ...");
            return false;
        }
        if (touch.type == EV_ABS && touch.code == ABS_X)
        {
            lcd->game_General->move_x = touch.value * lcd->lcd_w / 1024;
            all_x = touch.value * lcd->lcd_w / 1024;
        }

        if (touch.type == EV_ABS && touch.code == ABS_Y)
        {
            lcd->game_General->move_y = touch.value * lcd->lcd_h / 600;
            all_y = touch.value * lcd->lcd_h / 600;
        }
        if (lcd->game_General->move_x - 100 <= 0)
        {
            lcd->game_General->move_x = 100;
        }
        if (lcd->game_General->move_x + 100 >= 720)
        {
            lcd->game_General->move_x = 620;
        }
        if (touch.type == EV_KEY && touch.code == BTN_TOUCH && touch.value == 0)
        {
            btn_x = all_x;
            btn_y = all_y;
        }
        // 刷一个木板
        int *new_map = lcd->map_dev;
        for (int y = 460; y < 480; y++)
        {
            for (int x = 0; x < lcd->game_General->move_x - 100; x++)
            {
                *(new_map + 800 * y + x) = 0x00ffffff;
            }
        }
        for (int y = 460; y < 480; y++)
        {
            for (int x = lcd->game_General->move_x - 100; x < lcd->game_General->move_x + 100; x++)
            {
                *(new_map + 800 * y + x) = 0x000000ff;
            }
        }
        for (int y = 460; y < 480; y++)
        {
            for (int x = lcd->game_General->move_x + 100; x < 720; x++)
            {
                *(new_map + 800 * y + x) = 0x00ffffff;
            }
        }
        // //////////////////////
        // 功能按钮的实现
        if (btn_x > 720 && btn_x < 800 && btn_y > 0 && btn_y < 80)
        {
            lcd->game_General->ball_y0 = 0;
            lcd->game_General->ball_x0 = 0;
            lcd->game_General->score = 0;
            lcd->game_General->ball_flag = 0;
            lcd->game_General->game_difficulty = 600;
            // free(lcd->game_General);
            return true;
            printf("%d    %d退出111\n", all_x, all_y);
        }
        else if (btn_x > 720 && btn_x < 800 && btn_y > 80 && btn_y < 160)
        {
            if (lcd->game_General->ball_x0 == 0 || lcd->game_General->ball_y0 == 0)
            {
                get_rand(lcd);
            }
            if (lcd->game_General->score == 0)
            {
                int *new_map = lcd->map_dev;

                for (int y = 0; y < 16; y++)
                {
                    for (int x = 380; x < 420; x++)
                    {
                        *(new_map + 800 * y + x) = 0x00ffffff;
                    }
                }
                line(lcd, "score:0", 330, 0);
            }
            lcd->game_General->ball_flag = 1;
        }
        else if (btn_x > 720 && btn_x < 800 && btn_y > 160 && btn_y < 240)
        {
            lcd->game_General->score = 0;
            lcd->game_General->ball_flag = 0;
            lcd->game_General->ball_x0 = 0;
            lcd->game_General->ball_y0 = 0;
            get_rand(lcd);
            show_one_ball_arn(lcd);
            show_board_picter(lcd);
            printf("%dnandu结束\n", lcd->game_General->game_difficulty);
            line(lcd, "score:0", 330, 0);
        }
        else if (btn_x > 720 && btn_x < 800 && btn_y > 240 && btn_y < 320)
        {
            if (lcd->game_General->ball_flag != 0)
            {
                lcd->game_General->ball_flag = 0;
            }
            // printf("%dnandu暂停\n", lcd->game_General->game_difficulty);
        }
        else if (btn_x > 720 && btn_x < 800 && btn_y > 320 && btn_y < 400)
        {
            // printf("%dnandu++++\n", lcd->game_General->game_difficulty);
            lcd->game_General->game_difficulty -= 1000;
            if (lcd->game_General->game_difficulty <= 0)
            {
                lcd->game_General->game_difficulty = 0;
            }
            continue;
            // usleep(1000);
        }
        if (btn_x > 720 && btn_x < 800 && btn_y > 400 && btn_y < 480)
        {
            // printf("%d     %dnandu----\n", btn_x, btn_y);
            // printf("%dnandu----\n", lcd->game_General->game_difficulty);
            lcd->game_General->game_difficulty += 1000;
            if (lcd->game_General->game_difficulty > 5000)
            {
                lcd->game_General->game_difficulty = 5000;
            }
            continue;
            // usleep(1000);
        }
    }
    printf("jieshu-------\n");
    return true;
}

// ///////////////////////////////////////
// 创建一个结构体
bool game_info_bash(LCD_P lcd)
{
    GAME_P game_struct = (GAME_P)malloc(sizeof(GAME));
    if (game_struct == (GAME_P)NULL)
    {
        perror("创建game结构体失败\n");
        return false;
    }
    memset(game_struct, 0, sizeof(0));
    lcd->game_General = game_struct;
    // lcd->game_General->ball_y0 = 0;
    // lcd->game_General->ball_x0 = 0;
    lcd->game_General->ball_r = 50;
    lcd->game_General->ball_flag = 0;
    lcd->game_General->ball_mask_x = 1;
    lcd->game_General->ball_mask_y = 1;
    lcd->game_General->game_difficulty = 1000;
    get_rand(lcd);
    return true;
}
bool get_rand(LCD_P lcd)
{
    // 设置随机数种子
    srand((unsigned)time(NULL));
    while (1)
    {
        // 生成并打印一个随机数
        int randomNumber = rand() % 670;
        if (randomNumber > 50 && randomNumber < 670 && lcd->game_General->ball_x0 == 0)
        {
            lcd->game_General->ball_x0 = randomNumber;
            continue;
        }
        if (randomNumber > 80 && randomNumber < 300 && lcd->game_General->ball_y0 == 0)
        {
            lcd->game_General->ball_y0 = randomNumber;
            continue;
        }
        if ((lcd->game_General->ball_y0 != 0) && (lcd->game_General->ball_y0 != 0))
        {
            break;
        }
    }
    return true;
}
// 刷一个球
bool show_one_ball(LCD_P lcd)
{
    int *new_map = lcd->map_dev;
    printf("1\n");
    printf("%d     %d\n", lcd->game_General->ball_y0, lcd->game_General->ball_y0);
    for (int y = lcd->game_General->ball_y0 - lcd->game_General->ball_r; y <= lcd->game_General->ball_y0 + lcd->game_General->ball_r; y++)
    {
        printf("1ddadadasd\n");
        for (int x = lcd->game_General->ball_x0 - lcd->game_General->ball_r; x <= lcd->game_General->ball_x0 + lcd->game_General->ball_r; x++)
        {
            if ((x - lcd->game_General->ball_x0) * (x - lcd->game_General->ball_x0) + (y - lcd->game_General->ball_y0) * (y - lcd->game_General->ball_y0) < lcd->game_General->ball_r * lcd->game_General->ball_r)
            {

                *(new_map + lcd->lcd_w * y + x) = 0x0000ff00;
            }
            else
            {
                *(new_map + lcd->lcd_w * y + x) = 0x00ffffff;
            }
        }
        printf("3\n");
    }
    printf("2\n");
    return true;
}
// 球的线程函数
void *ball_running(void *arg)
{
    LCD_P lcd = (LCD_P)arg;
    int *new_map = lcd->map_dev;

    // if (lcd->game_General->score == 0)
    // {
    //     for (int y = 80; y < 380; y++)
    //     {
    //         for (int x = 160; x < 560; x++)
    //         {
    //             *(new_map + 800 * y + x) = 0x00ffffff;
    //         }
    //     }
    // }
    // printf("%d\n", lcd->game_General->ball_flag);
    while (1)
    {
        if (lcd->game_General->ball_flag == 0)
        {
            continue;
        }
        for (int y = lcd->game_General->ball_y0 - lcd->game_General->ball_r; y <= lcd->game_General->ball_y0 + lcd->game_General->ball_r, y < 460; y++)
        {
            for (int x = lcd->game_General->ball_x0 - lcd->game_General->ball_r - 5; x <= lcd->game_General->ball_x0 + lcd->game_General->ball_r + 5; x++)
            {
                if (((x - lcd->game_General->ball_x0) * (x - lcd->game_General->ball_x0) + (y - lcd->game_General->ball_y0) * (y - lcd->game_General->ball_y0) < lcd->game_General->ball_r * lcd->game_General->ball_r))
                {

                    *(new_map + lcd->lcd_w * y + x) = 0x0000ff00;
                }
                else
                {
                    *(new_map + lcd->lcd_w * y + x) = 0x00ffffff;
                }
            }
        }

        if (lcd->game_General->game_difficulty != 0)
        {
            usleep(lcd->game_General->game_difficulty);
            // printf("%d-----\n", lcd->game_General->game_difficulty);
        }

        // usleep(10);
        // printf("%d\n", lcd->game_General->game_difficulty);
        // for (int y = 14; y < 460; y++)
        // {
        //     for (int x = 0; x < 720; x++)
        //     {
        //         if ((x - lcd->game_General->ball_x0) * (x - lcd->game_General->ball_x0) + (y - lcd->game_General->ball_y0) * (y - lcd->game_General->ball_y0) < lcd->game_General->ball_r * lcd->game_General->ball_r)
        //         {
        //             *(new_map + lcd->lcd_w * y + x) = 0x0000ff00;
        //         }
        //         else
        //         {
        //             *(new_map + lcd->lcd_w * y + x) = 0x00ffffff;
        //         }
        //     }
        // }
        if (lcd->game_General->ball_x0 + lcd->game_General->ball_r >= 715)
            lcd->game_General->ball_mask_x = 0;
        if (lcd->game_General->ball_x0 - lcd->game_General->ball_r <= 0)
            lcd->game_General->ball_mask_x = 1;
        if (*(new_map + 800 * (lcd->game_General->ball_y0 + 50) + lcd->game_General->ball_x0) == 0x0000ff)
        {
            lcd->game_General->ball_mask_y = 0;
            lcd->game_General->score++;
            char num_buf[128];
            memset(num_buf, 0, sizeof(num_buf));
            sprintf(num_buf, "%s%d", "score:", lcd->game_General->score);
            line(lcd, num_buf, 330, 0);
        }
        if (lcd->game_General->ball_y0 - lcd->game_General->ball_r <= 14)
            lcd->game_General->ball_mask_y = 1;
        if (lcd->game_General->ball_mask_x == 1)
            lcd->game_General->ball_x0++;
        if (lcd->game_General->ball_mask_y == 1)
            lcd->game_General->ball_y0++;
        if (lcd->game_General->ball_mask_x == 0)
            lcd->game_General->ball_x0--;
        if (lcd->game_General->ball_mask_y == 0)
            lcd->game_General->ball_y0--;
        if (lcd->game_General->ball_y0 >= lcd->lcd_h + lcd->game_General->ball_r)
        {
            Show_One_Thumbnail(lcd, "/ui/game_over.bmp", 1, 160, 80);
            lcd->game_General->ball_y0 = 0;
            lcd->game_General->ball_x0 = 0;
            lcd->game_General->score = 0;
            lcd->game_General->ball_flag = 0;
            get_rand(lcd);
            sleep(2);
            show_one_ball_arn(lcd);
            show_board_picter(lcd);
        }
    }
    printf("结束\n");
    return NULL;
}
// 字体外的白屏
bool show_board_picter(LCD_P lcd)
{
    int *new_map = lcd->map_dev + 800 * 460;
    for (int y = 0; y < 20; y++)
    {
        for (int x = 520; x < 720; x++)
        {
            *(new_map + 800 * y + x) = 0x0000ff;
        }
    }
    return true;
}
// 除了功能键之外球的白屏
bool show_one_ball_arn(LCD_P lcd)
{
    int *new_map = lcd->map_dev;
    for (int y = 0; y < 480; y++)
    {
        for (int x = 0; x < 720; x++)
        {
            if ((x - lcd->game_General->ball_x0) * (x - lcd->game_General->ball_x0) + (y - lcd->game_General->ball_y0) * (y - lcd->game_General->ball_y0) < lcd->game_General->ball_r * lcd->game_General->ball_r)
            {

                *(new_map + lcd->lcd_w * y + x) = 0x0000ff00;
            }
            else
            {
                *(new_map + lcd->lcd_w * y + x) = 0x00ffffff;
            }
        }
    }
    return true;
}